package com.bandit.orm.test;

import com.btcode.orm.model.IField;
import com.btcode.orm.model.IModel;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import test.db.DBManager;

public class DataUnit {

    /**
     * 检查列表数据，是否在数据库中存在
     */
    public static boolean checkModelListExis(List<IModel> models, List<IField> excludeFields) {
        List<IModel> modelsCheck = new ArrayList<>();

        for (IModel modelItem : models) {
            IModel model = DBManager.getInstance().getORMExecuter().getModel(modelItem);
            if (model != null) {
                modelsCheck.add(model);
            }
        }

        if (modelsCheck.size() == 0) {
            return false;
        }

        return checkModelListEquip(models, modelsCheck, excludeFields);
    }

    /**
     * 检查两个modellist是否一致
     */
    public static boolean checkModelListEquip(List<?> modelsA, List<?> modelsB,
        List<IField> excludeFields) {

        if (modelsA == null && modelsB == null) {
            return true;
        }

        if (modelsA == null) {
            return false;
        }

        if (modelsB == null) {
            return false;
        }

        if (modelsA.size() == 0 && modelsB.size() == 0) {
            return true;
        }

        if (modelsA.size() == 0) {
            return false;
        }

        if (modelsB.size() == 0) {
            return false;
        }

        List<IField> fieldAs = ((IModel) modelsA.get(0)).getFields();

        if (excludeFields != null && excludeFields.size() > 0) {

            for (IField excludeField : excludeFields) {

                Iterator<IField> fieldIterator = fieldAs.iterator();

                while (fieldIterator.hasNext()) {
                    IField fieldA = fieldIterator.next();
                    if (excludeField.getFieldName().equals(fieldA.getFieldName())) {
                        fieldIterator.remove();
                    }
                }
            }

        }

        int bingo = 0;

        for (Object modelA : modelsA) {

            for (Object modelB : modelsB) {

                int valueBingo = 0;

                for (IField field : fieldAs) {
                    Object valueA = ((IModel) modelA).getFieldValue(field.getFieldName());
                    Object valueB = ((IModel) modelB).getFieldValue(field.getFieldName());

                    if (valueA == null && valueB == null) {
                        valueBingo++;
                        continue;
                    }

                    if (valueA == null) {
                        continue;
                    }

                    if (valueB == null) {
                        continue;
                    }

                    if (valueA.toString().equals(valueB.toString())) {
                        valueBingo++;
                        continue;
                    }
                }

                if (valueBingo == fieldAs.size()) {
                    bingo++;
                }

            }

        }

        if (bingo == modelsA.size()) {
            return true;
        }

        return false;
    }

}
